<template>
  <div>
    <el-form :inline="true">
      <el-form-item>
        <select-address
          v-model="params.address_code"
          @change="getData()"
          clearable
          placeholder="地址查询"
        ></select-address>
      </el-form-item>
      <el-form-item>
        <el-input
          placeholder="客户名称"
          @keyup.enter.native="getData()"
          clearable
          v-model="params.customer_name"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          placeholder="负责人"
          @keyup.enter.native="getData()"
          clearable
          v-model="params.corp_name"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="getData()">查询</el-button>
        <el-button icon="el-icon-plus" @click="handleEdit()">新增</el-button>
      </el-form-item>
    </el-form>
    <c-table border v-loading="loading" :columns="tableColumns" :data="tableData"></c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="tableCount"
        @current-change="getData"
      ></el-pagination>
    </div>
    <customer-file :visible.sync="visible" :customer-data="customerData"></customer-file>

    <new-customer
      :visible.sync="editVisible"
      :table-id="tableId"
      @success="getData(params.index)"
      :form-data="formData"
    ></new-customer>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { projectShowJobs } from '@/credit/plugins/constant'

import SelectAddress from '@/components/select/SelectAddress'
import CustomerFile from '@/credit/views/city/customer/CustomerFile'
import NewCustomer from '@/credit/views/city/customer/NewCustomer'

export default {
  components: {
    SelectAddress,
    CustomerFile,
    NewCustomer,
  },
  data() {
    return {
      order: {
        create_time: 'desc',
      },
      params: {
        index: 1,
        size: 15,
        'submit_type.eq': 2,
        'customer_type.eq': 1,
      },
      loading: false,
      tableColumns: [
        { attrs: { label: '客户名称', prop: 'customer_name', minWidth: '110px' } },
        { attrs: { label: '组织类型', prop: 'org_class_name', align: 'center' } },
        { attrs: { label: '地址', prop: 'address', minWidth: '110px' } },
        { attrs: { label: '负责人', prop: 'corp_name', align: 'center' } },
        { attrs: { label: '联系电话', prop: 'mobile_no', minWidth: '100px' } },
        {
          attrs: { label: '客户状态', prop: 'customer_status', align: 'center' },
          process: () => '-',
        },
        {
          attrs: { label: '担保贷款次数', prop: 'loan_count', align: 'center' },
          process: v => v || '-',
        },
        {
          attrs: { label: '纳税信用等级', prop: 'pay_tax_lever', align: 'center' },
          process: () => '-',
        },
        { attrs: { label: '评分', prop: 'pay_tax_lever', align: 'center' }, process: () => '-' },
        {
          attrs: { label: '操作', align: 'center' },
          operations: [
            { content: '编辑', icon: 'fa fa-edit', event: this.handleEdit },
            { content: '查看', icon: 'fa fa-search', event: this.handleView },
          ],
        },
      ],
      tableName: 'datum_customer_info_warehouse',
      tableData: [],
      tableCount: 0,
      visible: false,
      customerData: {},

      tableId: '',
      formData: {},
      editVisible: false,
    }
  },

  computed: {
    ...mapGetters(['clientId', 'job', 'userId']),
    isShowAllJob() {
      return projectShowJobs.includes(this.job)
    },
  },

  created() {
    this.params['service_id.eq'] = this.clientId

    if (!this.isShowAllJob) {
      this.params.res_a_b = this.userId
    }

    this.getData()
  },
  methods: {
    handleView(row) {
      this.customerData = row
      this.visible = true
    },
    getData(index = 1) {
      this.params.index = index
      this.loading = true
      this.$api
        .getData(this.$serviceName, this.tableName, this.params, this.order)
        .then(res => {
          const data = (res && res.data) || {}
          this.tableData = (data.data || []).map(this.dataAdapter)
          this.tableCount = data.totalCount || 0
        })
        .finally(() => {
          this.loading = false
        })
    },
    dataAdapter(item) {
      return item
    },
    handleEdit(row) {
      this.formData = row ? { ...row } : {}
      this.tableId = row ? row.datum_customer_info_warehouse_id : ''
      this.editVisible = true
    },
  },
}
</script>
<style lang="scss" scoped></style>
